package mi.payment.ui.print;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.ParentActivity;
import sunmi.payment.L3.L3BroadcastHelper;

/**
 * 交易打印
 */
public class TransactionPrintActivity extends ParentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.print_activity_main);
        initView();
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.menu_print);
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );

        // 补打上笔凭证
        View item = findViewById(R.id.item_print_last_transaction);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        item.findViewById(R.id.right_text).setVisibility(View.GONE);
        TextView leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.print_last_transaction);
        item.setOnClickListener(this);

        // 按凭证号打印
        item = findViewById(R.id.item_print_voucher_number);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        item.findViewById(R.id.right_text).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.print_voucher_number);
        item.setOnClickListener(this);

        // 打印交易明细
        item = findViewById(R.id.item_print_detail);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        item.findViewById(R.id.right_text).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.print_detail);
        item.setOnClickListener(this);

        // 打印结算总计
        item = findViewById(R.id.item_print_settlement_summary);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        item.findViewById(R.id.right_text).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.print_settlement_summary);
        item.setOnClickListener(this);

        // 打印前批次号的交易明细
        item = findViewById(R.id.item_print_last_settlement_detail);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        item.findViewById(R.id.right_text).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.print_last_settlement_detail);
        item.setOnClickListener(this);

        // 打印前批次号的结算总计
        item = findViewById(R.id.item_print_last_settlement_summary);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        item.findViewById(R.id.right_text).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.print_last_settlement_summary);
        item.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.item_print_last_transaction:
                PayDetail payDetail = mPayDetailModel.getLastSuccessTrade();
                if (payDetail == null) {
                    showToast(R.string.error_no_transaction);
                    return;
                }
                FillInTheCertificateActivity.startAction(this, null);
                break;
            case R.id.item_print_voucher_number:
                openActivity(PrintByDocumentNumberActivity.class);
                break;
            case R.id.item_print_detail:
                PrintTransactionDetailActivity.start(this, false);
                break;
            case R.id.item_print_settlement_summary:
                TotalSettlementDetailActivity.start(this, false);
                break;
            case R.id.item_print_last_settlement_detail:
                PrintTransactionDetailActivity.start(this, true);
                break;
            case R.id.item_print_last_settlement_summary:
                TotalSettlementDetailActivity.start(this, true);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        L3BroadcastHelper.sendSuccessBroadcast(this);
    }

    public static void startAction(Activity activity) {
        Intent intent = new Intent(activity, TransactionPrintActivity.class);
        activity.startActivity(intent);
    }


}
